
let Game = {
	/* 选择的游戏难度 */
	difficulty:'',
	/* 当前的游戏配置 */
	config: {},
	/* 玩家名字 */
	username: '',
	/*英雄*/
	hero:'',
	heromovespeed:10,
	bgm:'',

	/* 创建游戏 */
	create: function(mode,username){
		this.difficulty = mode;
		this.username = username;
		this.config = config.difficulties[mode];

		this.createEnemy();


		/*创建英雄,添加英雄飞机上下左右移动事件*/
		this.hero = $('#hero');
		let offset = this.hero.position()
		console.log(offset)
		$('body').keydown(e=>{
			e = e || window.event;
			e.preventDefault();
			let k = e.keyCode 
			let offset = this.hero.offset()
			let width = $('#battle-space').width();
			let height =  $('#battle-space').height();
			if(k==37){  //左
				if(offset.left > this.hero.width())
					this.hero.offset({top: offset.top,left:offset.left-this.heromovespeed})
				else
					this.hero.offset({top: offset.top,left:this.hero.width()/2})
			}else if(k==38){ //上
				if(offset.top>0)
					this.hero.offset({top: offset.top-this.heromovespeed,left:offset.left})
				else
					this.hero.offset({top:0,left:offset.left})
			}else if(k==39){ //右
				if(offset.left < width)
					this.hero.offset({top: offset.top,left:offset.left+this.heromovespeed})
				else
					this.hero.offset({top: offset.top,left:width- this.hero.width/2})
			}else if(k==40){ //下
				if(offset.top < height)
					this.hero.offset({top: offset.top + this.heromovespeed,left:offset.left})
				else
					this.hero.offset({top: height-this.hero.height(),left:offset.left})
			}
		})

		/*创建BGm*/
		this.bgm = new Audio('./sound/background.mp3');
		this.bgm.addEventListener('timeupdate', function(){
		    var buffer = 2.9;  //无缝播放可以调节这个参数达到最佳效果,不要使用标签，否则有停顿感
		    if(this.currentTime > this.duration - buffer){
		        this.currentTime = 0;
		        this.play();
		    }}, false);
		return this;
	},
	/* 开始游戏 */
	start: function(){
		/* 切换到战斗屏幕 */
		$('.screen').hide();
        $('#battle-space').show();
        /* 无缝播放BGM */
        //this.playMainBgm();
	},
	playMainBgm: function(){
		this.bgm.play()
	},
	pauseMainBgm: function(){
		this.bgm.pause();
	},
	/* 创建敌机 */
	createEnemy: function (){
		$('#battle-space').append('<div class="enemy"></div>')
	}

}